package com.aixingfu.sports.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aixingfu.sports.Adapter.MyFragmentAdapter;
import com.aixingfu.sports.MVP.contract.RenewContract;
import com.aixingfu.sports.MVP.presenter.RenewPresenter;
import com.aixingfu.sports.Model.RenewModel;
import com.aixingfu.sports.R;
import com.aixingfu.sports.ui.fragment.IncreaseValidTimeFragment;
import com.aixingfu.sports.ui.fragment.RenewFragment;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.orie.librarybase.base.BaseActivity;
import com.vondear.rxtool.RxTimeTool;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

//续费
@Route(path = "/main/RenewActivity")
public class RenewActivity extends BaseActivity implements RenewContract.MyView {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_card_type)
    TextView tvCardType;
    @BindView(R.id.tv_birthday)
    TextView tvBirthday;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_member_code)
    TextView tvMemberCode;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    private String memberCardId;
    private String memberId;
    private String card_number;
    private RenewPresenter renewPresenter;
    private static final String[] CHANNELS = new String[]{"普通续费", "增加卡有效期"};
    private List<String> mDataList = Arrays.asList(CHANNELS);
    private List<Fragment> listFragment = new ArrayList<>();
    private MyFragmentAdapter myFragmentAdapter;
    private String card_name, invalid_time, username, mobile;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_renew;
    }

    public static void startActivityByRoute() {
        ARouter.getInstance().build("/main/RenewActivity").navigation();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        tvTitle.setText("续费");
        listFragment.add(RenewFragment.newInstance("普通续费", ""));
        listFragment.add(IncreaseValidTimeFragment.newInstance("增加卡有效期", ""));
        myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), listFragment, CHANNELS);
        viewPager.setAdapter(myFragmentAdapter);
        magicIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(RenewActivity.this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setNormalColor(Color.BLACK);
                simplePagerTitleView.setSelectedColor(R.color.main_color);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(R.color.main_color);
                return linePagerIndicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);

        Bundle bundle = getIntent().getExtras();
        memberCardId = bundle.getString("memberCardId", "");
        memberId = bundle.getString("memberId", "");
        card_name = bundle.getString("card_name", "");
        invalid_time = bundle.getString("invalid_time", "");
        username = bundle.getString("username", "");
        mobile = bundle.getString("mobile", "");

        card_number = bundle.getString("card_number", "");
        renewPresenter = new RenewPresenter();
        renewPresenter.attachView(this);
        getRenew();
        tvName.setText("会员姓名：" + username);
        tvPhone.setText("手机号：" + mobile);
        tvCardType.setText("卡种名称：" + card_name);
        tvMemberCode.setText("会员卡号：" + card_number);
        tvTime.setText("到期时间：" + RxTimeTool.stampToDate(invalid_time));
    }

    public void getRenew() {
        if (renewPresenter.isViewAttached()) {
            HashMap<String, String> map = new HashMap<>();
            map.put("member_card_id", memberCardId);
            map.put("member_id", memberId);
            map.put("type", "1");
            renewPresenter.getRenew(map);
        }
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void beforeInit() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showToast(String msg, String state) {

    }

    @Override
    public void setRenew(RenewModel data) {
        if (data.getCode() == 1) {
            RenewModel.DataBean.MemberInfoBean member_info = data.getData().getMember_info();
            tvBirthday.setText("剩余天数：" + member_info.getArtive_day());
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .dontAnimate()
                    .placeholder(R.mipmap.touxiang)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(RenewActivity.this)
                    .load(member_info.getAvatar_url())
                    .apply(options)
                    .into(ivHead);
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void hideLoading() {
        dismissLoadingDialog();
    }

    @Override
    public Context getMyContext() {
        return this;
    }
}
